<template>
  <view class="phone-dialog" v-show="visible" @click.stop>
    <view class="dialog-mask" @tap="closeDialog"></view>
    <view class="dialog-content">
      <view class="dialog-header">
        <text class="dialog-title">输入手机号 </text>
        <view v-if="!post" class="close-btn" @tap="closeDialog">
          <u-icon name="close" color="#999" size="20"></u-icon>
        </view>
      </view>

      <view class="dialog-body">
        <view class="input-section">
          <view class="input-wrapper">
            <u-input
              v-model="phoneNumber"
              placeholder="请输入手机号码"
              class="phone-input"
              type="number"
              maxlength="11"
              @input="validatePhone"
            />
          </view>
          <text v-if="phoneError" class="error-text">{{ phoneError }}</text>
        </view>

        <view class="tip-section">
          <text class="tip-text">* 必须填写手机号才能查看测算报告</text>
        </view>
      </view>

      <view class="dialog-footer">
        <view
          class="submit-btn"
          :class="{ disabled: !isValidPhone }"
          @tap="handleSubmit"
        >
          <text class="submit-text">提交</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { bindUserPhone } from "@/api/fortune";
import { mapActions } from "vuex";

export default {
  name: "PhoneInputDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    post: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      phoneNumber: "",
      phoneError: "",
      isValidPhone: false,
    };
  },
  methods: {
    ...mapActions("system", ["setUserPhone"]),

    // 验证手机号
    validatePhone(value) {
      this.phoneNumber = value;
      const phoneRegex = /^1[3-9]\d{9}$/;

      if (!value) {
        this.phoneError = "请输入手机号码";
        this.isValidPhone = false;
      } else if (!phoneRegex.test(value)) {
        this.phoneError = "请输入正确的手机号码";
        this.isValidPhone = false;
      } else {
        this.phoneError = "";
        this.isValidPhone = true;
      }
    },

    // 提交手机号
    async handleSubmit() {
      if (!this.isValidPhone) {
        this.$show.showToast("请输入正确的手机号码");
        return;
      }

      if (this.post) {
        try {
          uni.showLoading({ title: "正在绑定...", mask: true });
          const res = await bindUserPhone(this.phoneNumber);

          if (res.code === 0) {
            // 保存到vuex
            this.setUserPhone(this.phoneNumber);

            this.$show.showToast("绑定成功", {
              icon: "success",
              success: () => {
                this.$emit("success", this.phoneNumber);
                this.closeDialog();
              },
            });
          } else {
            this.$show.showToast(res.message || "绑定失败，请重试");
          }
        } catch (error) {
          this.$show.showToast("绑定失败，请重试");
        } finally {
          uni.hideLoading();
        }
      } else {
        this.$emit("success", this.phoneNumber);
      }
    },

    // 关闭弹窗
    closeDialog() {
      this.$emit("close");
      this.resetForm();
    },

    // 重置表单
    resetForm() {
      this.phoneNumber = "";
      this.phoneError = "";
      this.isValidPhone = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.phone-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.dialog-content {
  position: relative;
  width: 600rpx;
  background: #fff;
  border-radius: 20rpx;
  overflow: hidden;
  z-index: 10000;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 40rpx 40rpx 20rpx;
  border-bottom: 1px solid #f0f0f0;
}

.dialog-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.close-btn {
  padding: 10rpx;
}

.dialog-body {
  padding: 20rpx 40rpx 0;
}

.input-section {
  margin-bottom: 30rpx;
}

.input-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 20rpx;
  font-weight: 500;
}

.input-wrapper {
  // border: 2rpx solid #e0e0e0;
  // border-radius: 12rpx;
  // padding: 0 20rpx;
  // background: #fafafa;
}

.phone-input {
  height: 80rpx;
  font-size: 32rpx;
  color: #333;
}

.error-text {
  display: block;
  font-size: 24rpx;
  color: #ff4757;
  margin-top: 10rpx;
}

.tip-section {
  margin-bottom: 30rpx;
}

.tip-text {
  font-size: 24rpx;
  color: #dd302c;
  line-height: 1.5;
}

.dialog-footer {
  padding: 0 40rpx 40rpx;
}

.submit-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #dd302c 0%, #ff6b35 100%);
  border-radius: 44rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 20rpx rgba(221, 48, 44, 0.3);

  &.disabled {
    background: gray;
    box-shadow: none;
  }

  &:active {
    transform: scale(0.98);
  }
}

.submit-text {
  font-size: 32rpx;
  color: #fff;
  font-weight: 500;
}
</style>
